package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.SimpleFooter;
import com.mc.developmentkit.views.SimpleHeader;
import com.mc.developmentkit.views.SpringView;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.CommentariesBean;
import com.wude.guild.R;
import com.wude.guild.adapter.CommentariesAdapter;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/23 0023.
 * <p>
 * 动态
 */

@SuppressLint("HandlerLeak")
public class CommentariesFragment extends TitleBaseFragment {
    private SpringView springview;
    private RecyclerView recyclerView;
    private int limit = 1;
    private List<CommentariesBean> dataList = new ArrayList<>();
    private CommentariesAdapter adapter;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.commentaries_fragment, container, false);
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("CommentariesFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("CommentariesFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        setHeaderTitle("动态");
        getTitleHeaderBar().getLeftImageView().setVisibility(View.GONE);
        recyclerView = view.findViewById(R.id.recyclerView);
        springview = view.findViewById(R.id.springview);

        initAndReflsh();

        //设置layoutManager
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        adapter = new CommentariesAdapter(new ArrayList<CommentariesBean>(), getContext());
        recyclerView.setAdapter(adapter);
        //设置item之间的间隔
        SpacesItemDecoration decoration = new SpacesItemDecoration(30);
        recyclerView.addItemDecoration(decoration);

        springview.setType(SpringView.Type.FOLLOW);
        springview.setListener(onFreshListener);
        springview.setHeader(new SimpleHeader(getActivity()));
        springview.setFooter(new SimpleFooter(getActivity()));
    }

    SpringView.OnFreshListener onFreshListener = new SpringView.OnFreshListener() {
        @Override
        public void onRefresh() {
            initAndReflsh();
            adapter.setItemOnclick(false);
        }

        @Override
        public void onLoadmore() {
            onLoadMord();
            adapter.setItemOnclick(false);
        }
    };

    private void initAndReflsh() {
        dataList.clear();
        limit = 1;
        Map<String, String> map = new HashMap<>();
        map.put("row", "20");
        map.put("limit ", limit + "");
        HttpCom.POST(handler, HttpCom.getCommentaries(), map, false);
    }

    private void onLoadMord() {
        limit = ++limit;
        Map<String, String> map = new HashMap<>();
        map.put("sdk_version", "1");
        map.put("limit ", limit + "");
        HttpCom.POST(mhandler, HttpCom.getCommentaries(), map, false);
    }


    /**
     * 刚进来请求
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<CommentariesBean> pinglunBeen = HttpUtils.DNSCommentaries(msg.obj.toString());
                    if (pinglunBeen != null) {
                        dataList.addAll(pinglunBeen);
                        adapter.setDataList(dataList);
                    } else {
                        springview.setVisibility(View.GONE);
                        ToastUtil.showToast("没有数据啊！");
                    }
                    adapter.setItemOnclick(true);
                    break;
                case 2:
                    springview.setVisibility(View.GONE);
//                    errorLayout.setVisibility(View.VISIBLE);
//                    errorText.setText("网络异常");
                    break;
            }
        }
    };

    /**
     * 加载更多请求
     */
    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            springview.onFinishFreshAndLoad();
            switch (msg.what) {
                case 1:
                    ArrayList<CommentariesBean> pinglunBeen = HttpUtils.DNSCommentaries(msg.obj.toString());
                    if (pinglunBeen != null) {
                        dataList.addAll(pinglunBeen);
                        adapter.setDataList(dataList);
                    } else {
                        ToastUtil.showToast("已经到底了~");
                    }
                    adapter.setItemOnclick(true);
                    break;
                case 2:
                    break;
            }
        }
    };
}
